package com.md.album.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.md.album.R
import com.md.album.bean.SelfWrapperAudio
import com.md.album.db.SelfAlbumManager
import com.md.album.event.SelfRefreshEvent
import com.md.basedpc.constant.BaseConstant.SELF_CREATE_ALBUM
import com.md.basedpc.routerservice.NotifyService
import org.greenrobot.eventbus.EventBus

class SelectAudioAdapter(mContext: Context, audioList:List<SelfWrapperAudio>):RecyclerView.Adapter<SelectAudioAdapter.SelectAudioViewHolder>() {

    private var mContext=mContext
    private var audioList = audioList
    private var albumId= -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelectAudioViewHolder {
        var view = LayoutInflater.from(mContext).inflate(R.layout.adapter_select_audio,parent,false)
        return SelectAudioViewHolder(view)
    }

    override fun getItemCount(): Int {
        return audioList.size
    }

    override fun onBindViewHolder(holder: SelectAudioViewHolder, position: Int) {
        var wrapperAudio = audioList[position]
        holder.tvNum?.text= wrapperAudio.audio.id
        holder.tvTitle?.text= wrapperAudio.audio.name
        holder.tvAuthor?.text= wrapperAudio.audio.author
        holder.cltRoot?.setOnClickListener {
            var isSuc = SelfAlbumManager.getIns().saveAudioToAlbum(albumId.toString(),wrapperAudio.audio)
            if (isSuc){
                EventBus.getDefault().post(SelfRefreshEvent())
                ARouter.getInstance().navigation(NotifyService::class.java).selfAlbumUpdateSuc(SELF_CREATE_ALBUM)
                showIvMore(false,holder)
                holder.cltRoot?.isClickable = false
            }
        }
        if (wrapperAudio.isAdd){
            showIvMore(false,holder)
            holder.cltRoot?.isClickable = false
        }else{
            showIvMore(true,holder)
            holder.cltRoot?.isClickable = true
        }

    }


    private inline fun showIvMore(isShow: Boolean, holder: SelectAudioViewHolder){
        if (isShow){
            holder.ivMore?.visibility= View.VISIBLE
            holder.tvAdded?.visibility= View.GONE
        }else{
            holder.ivMore?.visibility= View.GONE
            holder.tvAdded?.visibility= View.VISIBLE
        }
    }

    fun setAlbumID( albumId: Int){
        this.albumId = albumId
    }

    public fun setData(audioList:List<SelfWrapperAudio>){
        this.audioList = audioList
        notifyDataSetChanged()
    }


    class SelectAudioViewHolder:RecyclerView.ViewHolder{
        var cltRoot:ConstraintLayout?=null
        var tvNum:TextView?=null
        var ivMore:ImageView?=null
        var tvAdded:TextView?=null
        var tvTitle:TextView?=null
        var tvAuthor:TextView?=null

        constructor(view:View):super(view){
            cltRoot = view.findViewById(R.id.cltRoot)
            tvNum = view.findViewById(R.id.tvAuthor)
            ivMore = view.findViewById(R.id.ivMore)
            tvAdded = view.findViewById(R.id.tvAdded)
            tvTitle = view.findViewById(R.id.tvTitle)
            tvAuthor = view.findViewById(R.id.tvAuthor)
        }

    }
}